package com.cskaoyan.controller.wx;

import com.cskaoyan.bean.bo.*;
import com.cskaoyan.bean.pojo.MallUser;
import com.cskaoyan.bean.vo.BaseRespVo;
import com.cskaoyan.bean.vo.WxCartIndexVO;
import com.cskaoyan.bean.vo.WxCheckOutVO;
import com.cskaoyan.bean.vo.WxCouponList;
import com.cskaoyan.service.WxCartService;
import com.cskaoyan.service.WxCouponService;
import com.fasterxml.jackson.databind.ser.Serializers;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author: Fengyi
 * @description:
 * @time: 2021/7/12 17:03
 */
@RequestMapping("wx/cart")
@RestController
public class WxCartController {

    @Autowired
    WxCartService wxCartService;
    @Autowired
    WxCouponService wxCouponService;

    @RequestMapping("index")
    public BaseRespVo cartIndex() {
       WxCartIndexVO wxCartIndexVO =  wxCartService.queryIndex();
       return BaseRespVo.ok(wxCartIndexVO);
    }

    @RequestMapping("checked")
    public BaseRespVo checkedCart(@RequestBody WxCartCheckedBO wxCartCheckedBO) {
        WxCartIndexVO wxCartIndexVO =  wxCartService.checkedCart(wxCartCheckedBO);
        return BaseRespVo.ok(wxCartIndexVO);
    }

    @RequestMapping("update")
    public BaseRespVo updateCart(@RequestBody WxCartUpdateBO wxCartUpdateBO) {
        int code = wxCartService.updateCart(wxCartUpdateBO);

        if(code == 404) {
            return BaseRespVo.NotEnoughStorage1("库存不足！");
        }
        return BaseRespVo.ok();
    }

    @RequestMapping("goodscount")
    public BaseRespVo goodscount() {
        Subject subject = SecurityUtils.getSubject();
        if(subject.getPrincipals() == null) {
            return BaseRespVo.ok(0);
        }
        MallUser user = (MallUser) subject.getPrincipals().getPrimaryPrincipal();



        int count = wxCartService.goodsCount();
        return BaseRespVo.ok(count);
    }

    @RequestMapping("delete")
    public BaseRespVo deleteCartGoods(@RequestBody WxDeleteCartGoodsBO wxDeleteCartGoodsBO) {
        wxCartService.deleteCartGoods(wxDeleteCartGoodsBO);
        return BaseRespVo.ok();
    }

    @RequestMapping("add")
    public BaseRespVo addGoodsToCart(@RequestBody WxAddGoodsToCartBO wxAddGoodsToCartBO) {

        int code = wxCartService.addGoodsToCart(wxAddGoodsToCartBO);
        if(code == 404) {
            return BaseRespVo.NotEnoughStorage("库存不足");
        }
        int count = wxCartService.goodsCount();
        return BaseRespVo.ok(count);
    }

    @RequestMapping("fastadd")
    public BaseRespVo fastAdd(@RequestBody WxAddGoodsToCartBO wxAddGoodsToCartBO) {

        int cartId = wxCartService.fastAdd(wxAddGoodsToCartBO);

        return BaseRespVo.ok(cartId);
    }

    @RequestMapping("checkout")
    public BaseRespVo checkout(WxCheckedOutBO wxCheckedOutBO) {
        WxCheckOutVO wxCheckOutVO = wxCartService.checkOut(wxCheckedOutBO);




        List<WxCouponList> wxCouponLists = wxCouponService.selectlist(wxCheckedOutBO.getCartId());

        if(wxCouponLists == null) {
            wxCheckOutVO.setAvailableCouponLength(0);
        }else {
            if(wxCheckOutVO.getCouponId() == 0) {
                wxCheckOutVO.setCouponId(-1);
            }
            wxCheckOutVO.setAvailableCouponLength(wxCouponLists.size());

        }



        return BaseRespVo.ok(wxCheckOutVO);
    }



}
